五、网络配置
5.1 获取 IP 地址
系统默认使用 DHCP 自动获取 IP 地址。如需获取设备的 IP 地址,请使用 ifconfig 命令:
root@taco-mes20:~# ifconfig
...
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.56.220 netmask 255.255.254.0 broadcast 192.168.57.255
inet6 fe80::ffcb:7fab:c1f1:7ba prefixlen 64 scopeid 0x20<link>
ether ea:97:b3:36:a8:8a txqueuelen 1000 (Ethernet)
RX packets 261813 bytes 88985717 (88.9 MB)
RX errors 1023 dropped 9198 overruns 0 frame 1023
TX packets 66161 bytes 4517331 (4.5 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 32
...
此时 eth0 的 IP 地址为 192.168.56.220。
5.2 配置静态 IP
在配置静态 IP 之前,需要选择一个未被使用的 IP 地址,可以通过 ping 命令检查某个 IP 地址是否已经被使用。
如需手动配置静态 IP,请修改 /etc/netplan/01-network-manager-all.yaml 配置文件。以下是一个示例配置文件:
network:
version: 2
renderer: NetworkManager
ethernets:
eth0:
dhcp4: no
addresses:
- 192.168.150.1/24
optional: yes
dhcp-identifier: mac
routes:
- to: default
via: 192.168.150.254
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
eth1:
dhcp4: no
addresses:
- 192.168.56.5/24
optional: yes
dhcp-identifier: mac
routes:
- to: default
via: 192.168.57.254
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
eth2:
dhcp4: no
addresses:
- 192.168.56.6/24
optional: yes
dhcp-identifier: mac
routes:
- to: default
via: 192.168.57.254
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
eth3:
dhcp4: no
addresses:
- 192.168.56.7/24
optional: yes
routes:
- to: default
via: 192.168.57.254
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
保存文件后,执行以下命令应用配置:
netplan apply
这将为 eth0、eth1、eth2 和 eth3 配置静态 IP 地址。
5.3 验证配置
如需验证静态 IP 配置是否生效,可使用 netplan try 命令:
root@taco-mes20:~# netplan try
Do you want to keep these settings?
Press ENTER before the timeout to accept the new configuration
Changes will revert in 117 seconds
Configuration accepted.
可通过 ifconfig 命令查看配置文件中定义的 IP 地址是否已激活。
5.4 动态 IP 配置
设置动态 IP 之前,需要先将 /etc/netplan/01-network-manager-all.yaml 文件中的静态 IP 配置注释掉,然后再进行配置:
network:
version: 2
renderer: NetworkManager
# ethernets:
# eth0:
# dhcp4: no
# addresses:
# - 192.168.150.1/24
# optional: yes
# dhcp-identifier: mac
# routes:
# - to: default
# via: 192.168.150.254
# nameservers:
# addresses:
# - 8.8.8.8
# - 8.8.4.4
# eth1:
# dhcp4: no
# addresses:
# - 192.168.56.5/24
# optional: yes
# dhcp-identifier: mac
# routes:
# - to: default
# via: 192.168.57.254
# nameservers:
# addresses:
# - 8.8.8.8
# - 8.8.4.4
# eth2:
# dhcp4: no
# addresses:
# - 192.168.56.6/24
# optional: yes
# dhcp-identifier: mac
# routes:
# - to: default
# via: 192.168.57.254
# nameservers:
# addresses:
# - 8.8.8.8
# - 8.8.4.4
# eth3:
# dhcp4: no
# addresses:
# - 192.168.56.7/24
# optional: yes
# routes:
# - to: default
# via: 192.168.57.254
# nameservers:
# addresses:
# - 8.8.8.8
# - 8.8.4.4
如果系统未能获取 IP 地址,可能是因为 DHCP 服务器未运行或网络未连接。可以使用以下命令手动请求 IP:
dhclient -v eth0
这将触发 DHCP 请求并尝试从 DHCP 服务器获取 IP 地址。
如果网络无法连通,请使用系统命令训练 ETH PHY 参数,具体步骤如下:
- 执行
tps-eth --list查询系统识别的网络:
root@taco-mes20:~# tps-eth --list
Net name needed to adjust delay line:
eth0
eth1
eth2
eth3
- 选择训练某个 ETH 网口:
root@taco-mes20:~# tps-eth eth0
MAC: a6:fa:7d:47:ee:62
Speed: 1000Mbps
Elapsed time: 3126 ms
Continuous cnt and location, tx: 58 71, rx: 47 46
Proper tx and rx: 42, 22
- 输入
systemctl restart NetworkManager来立即获取 IP,使用ifconfig或ip a命令检查 IP 地址:
root@taco-mes20:~# ifconfig
eth0: flags=4163 mtu 1500
inet 192.168.56.220 netmask 255.255.254.0 broadcast 192.168.57.255
inet6 fe80::ffcb:7fab:c1f1:7ba prefixlen 64 scopeid 0x20
ether ea:97:b3:36:a8:8a txqueuelen 1000 (Ethernet)
RX packets 261813 bytes 88985717 (88.9 MB)
RX errors 1023 dropped 9198 overruns 0 frame 1023
TX packets 66161 bytes 4517331 (4.5 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 32